﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_CHITIETTHUEMAY
    {
        static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //Them
        public static bool ThemCTTM(ref string err,int MaPhieu,string MaMay,string GioBatDau,float GiaThueMay)
        {
            bool them = false;
            CHITIETTHUEMAY cttm = new CHITIETTHUEMAY();
            try
            {
                cttm.MaPhieu = MaPhieu;
                cttm.MaMay = MaMay;
                cttm.GioBatDau = GioBatDau;
                cttm.GiaThueMay = GiaThueMay;
                cttm.DaXoa = false;
                db.CHITIETTHUEMAYs.InsertOnSubmit(cttm);
                db.SubmitChanges();
                them = true;

            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        //Cap Nhat
        public static bool CapNhatCTTM(ref string err,int MaPhieu,string MaMay,string GioBatDau,string GioKetThuc,float GiaThueMay)
        {
            bool update=false;
            try
            {
                CHITIETTHUEMAY tim=db.CHITIETTHUEMAYs.Single(cttm=>cttm.MaMay.Equals(MaMay)&&cttm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    tim.GioBatDau=GioBatDau;
                    tim.GioKetThuc=GioKetThuc;
                    tim.GiaThueMay=GiaThueMay;
                    db.SubmitChanges();
                    update=true;
                }
            }
            catch(Exception e)
            {
                err=e.Message;
            }
            return update;
        }
        //Cap nhat khi thanh toan
        public static bool CapNhatThanhToan(ref string err, int MaPhieu, string MaMay,  string GioKetThuc)
        {
            bool update = false;
            try
            {
                CHITIETTHUEMAY tim = db.CHITIETTHUEMAYs.Single(cttm => cttm.MaMay.Equals(MaMay) && cttm.MaPhieu.Equals(MaPhieu));
                if (tim != null)
                {
                   
                    tim.GioKetThuc = GioKetThuc;
                    
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //Xoa
        public static bool XoaCTTM(ref string err,int MaPhieu,string MaMay)
        {
            bool xoa = false;
            try
            {
                CHITIETTHUEMAY tim=db.CHITIETTHUEMAYs.Single(cttm=>cttm.MaMay.Equals(MaMay)&&cttm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    db.CHITIETTHUEMAYs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err,int MaPhieu,string MaMay,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                CHITIETTHUEMAY tim=db.CHITIETTHUEMAYs.Single(cttm=>cttm.MaMay.Equals(MaMay)&&cttm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }

        //ConverToDataTable

        public static DataTable ConverToDataTable(List<CHITIETTHUEMAY> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaPhieu");
            dt.Columns.Add("MaMay");
            dt.Columns.Add("GioBatDau");
            dt.Columns.Add("GioKetThuc");
            dt.Columns.Add("GiaThueMay");
            dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaPhieu, i.MaMay, i.GioBatDau, i.GioKetThuc, i.GiaThueMay,i.DaXoa);
            }
            return dt;
        }
        public static DataTable LayThongTinCTTM()
        {
            var cttm = from ct in db.CHITIETTHUEMAYs select ct;
            return ConverToDataTable(cttm.ToList());
        }
        //Lấy mã phiếu
        public static int LayMaPhieu(string MaMay)
        {
            int maphieu = 0;
            CHITIETTHUEMAY cttm = new CHITIETTHUEMAY();
            try
            {
                cttm=db.CHITIETTHUEMAYs.Single(tm=>tm.MaMay.Equals(MaMay) && tm.GioKetThuc==null);
                maphieu = int.Parse(cttm.MaPhieu.ToString());
            }
            catch(Exception)
            {

            }
            return maphieu;

        }
        public static double SoGioSuDung(int MaPhieu,string MaMay)
        {
            TimeSpan sogio = new TimeSpan();
            CHITIETTHUEMAY cttm = new CHITIETTHUEMAY();
            try
            {
                cttm = db.CHITIETTHUEMAYs.Single(tm => tm.MaPhieu.Equals(MaPhieu) && tm.MaMay.Equals(MaMay));
                DateTime.Parse(cttm.GioKetThuc).Subtract(DateTime.Parse(cttm.GioBatDau));
                sogio = DateTime.Parse(cttm.GioKetThuc).Subtract(DateTime.Parse(cttm.GioBatDau));
            }
            catch (Exception)
            {

            }

            return Math.Round(float.Parse(sogio.Ticks.ToString()) / 36000000000, 1);
        }
        
    }
}
